package com.harmontronics.hanma.paas.paas.domain.releasesetup.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.harmontronics.hanma.paas.paas.consts.EnvEnum;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.valueobject.HmReleaseSetupFields;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.repository.persistence.HmReleaseSetupRepository;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.HmReleaseSetup;
import com.harmontronics.hanma.paas.paas.utils.MyBeanUtils;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.harmontronics.hanma.paas.paas.domain.releasesetup.repository.mapper.HmReleaseSetupMapper;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.repository.po.HmReleaseSetupPO;

@Service("hmReleaseSetupService")
public class HmReleaseSetupService {

    @Autowired
    private HmReleaseSetupRepository hmReleaseSetupRepository;

    @Autowired
    private HmReleaseSetupMapper hmReleaseSetupMapper;



    public HmReleaseSetup findById(String appUnique, EnvEnum setupType) {
        QueryWrapper<HmReleaseSetupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmReleaseSetupFields.APP_UNIQUE_ID,appUnique).
                eq(HmReleaseSetupFields.SETUP_TYPE,setupType);
        HmReleaseSetupPO po = hmReleaseSetupMapper.selectOne(queryWrapper);
        return BeanUtil.toBean(po,HmReleaseSetup.class);
    }

    /**
     * 添加默认记录
     * @param appUnique
     * @param setupType
     */
    public void addDefault(String appUnique,EnvEnum setupType){
        HmReleaseSetupPO setupPO=new HmReleaseSetupPO();
        setupPO.setAppUniqueId(appUnique);
        setupPO.setSetupType(setupType);
        setupPO.setHostPort("80");
        hmReleaseSetupRepository.save(setupPO);
    }

    public void save(HmReleaseSetup hmReleaseSetup) {
            HmReleaseSetupPO po = BeanUtil.toBean(hmReleaseSetup,HmReleaseSetupPO.class);
            hmReleaseSetupRepository.save(po);
    }

    public void update(HmReleaseSetup hmReleaseSetup) {
            HmReleaseSetupPO po = BeanUtil.toBean(hmReleaseSetup,HmReleaseSetupPO.class);
            if(po.getRedisPassword() == null){
                po.setRedisPassword("");
            }
            if(po.getDatabasePassword() == null){
                po.setDatabasePassword("");
            }
            hmReleaseSetupRepository.updateById(po);
    }

    public List<HmReleaseSetup> getSetupBySetupType(String appUnique,EnvEnum setupType){
        QueryWrapper<HmReleaseSetupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmReleaseSetupFields.APP_UNIQUE_ID,appUnique)
                .eq(HmReleaseSetupFields.SETUP_TYPE,setupType);
        List<HmReleaseSetupPO> releaseSetupPOList = hmReleaseSetupRepository.list(queryWrapper);
        return MyBeanUtils.batchTransform(HmReleaseSetup.class,releaseSetupPOList);
    }

}